#/bin/bash
#sqamt  快捷告警管理工具
#环境变量, python,node,mysql,email


#项目根目录
baseDir=/usr/local/shell/alter
#晚上休息时间
morningTime=$(date +%s -d "`date +%F' '08:00:00`")
nightTime=$(date +%s -d "`date +%F' '22:00:00`")
#午休时间
noonstartTime=$(date +%s -d "`date +%F' '12:00:00`")
noonstopTime=$(date +%s -d "`date +%F' '13:30:00`")

#数据库
mysqlHost="81.68.254.79"
mysqlUser="root"
mysqlPassword="6DXb7JSQ6yYHd6Hi4t8jBGJZlcR3QomnzF3cGXKTawk="
mysqlDatabase="9acmdb"
mysqlxx="pymysql.Connect("81.68.254.79", "root", "6DXb7JSQ6yYHd6Hi4t8jBGJZlcR3QomnzF3cGXKTawk=", "9acmdb",charset='utf8', cursorclass=pymysql.cursors.DictCursor, autocommit=True)"

#邮箱
emailUser="18820304608@139.com"
emailPassword=""

#钉钉token
webhook="https://oapi.dingtalk.com/robot/send?access_token=02fd5264b64d3907ec768ad1b133f739cb5720e7e90148d4e6ca37cf34a83a94"
#altermangerUrl,user,passwd
alterSendUrl="http://alterurl"
alterManagerUrl="http://alterManagerurl"
alterManagerUser="alterManagerUser"
alterManagerPassword="alterManagerPassword"


#邮箱配置



sqamt_env (){
#修改时间
sed -i "s#morningTime=$(date +%s -d "`date +%F' '08:00:00`")#$morningTime#g" shell/alarmProcess
sed -i "s#nightTime=$(date +%s -d "`date +%F' '22:00:00`")#$nightTime#g" shell/alarmProcess
sed -i "s#noonstartTime=$(date +%s -d "`date +%F' '12:00:00`")#$noonstartTime#g" shell/alarmProcess
sed -i "s#noonstopTime=$(date +%s -d "`date +%F' '13:30:00`")#$noonstopTime#g" shell/alarmProcess


#修改token
sed -i "s#noonstopTime=$(date +%s -d "`date +%F' '13:30:00`")#$noonstopTime#g" libs/send.py
sed -i 's#http://alterurl#$alterurl#g' libs/send.py
sed -i 's#http://alterurl#$alterurl#g' libs/getPromethosData.py
sed -i 's#http://alterurl#$alterurl#g' libs/getPromethosData.py
sed -i 's#http://alterurl#$alterurl#g' libs/getPromethosData.py

sed -i "s#noonstopTime=$(date +%s -d "`date +%F' '13:30:00`")#$noonstopTime#g" libs/alterDate.py
sed -i "s#noonstopTime=$(date +%s -d "`date +%F' '13:30:00`")#$noonstopTime#g" libs/alterDate.py

#修改数据库连接信息
sed -i "s#https://oapi.dingtalk.com/robot/send?access_token=#$webhook#g" libs/alterData.py

#项目目录变更    
mkdir -p $baseDir
mv QAM    $baseDir
crontab -l >$baseDir/crontab
echo "每分钟运行一次获取告警数据脚本"
echo "* * * * * python3 $baseDir/libs/getPromethosData.py > /dev/null 2>&1" >>$baseDir/crontab
crontab $baseDir/crontab

pip -r reuqust.txt
echo "请修改告警排班表"
}

gt_python(){
echo -e "\033[32m [INFO]: Start install python3 \033[0m"
yum update -y
yum groupinstall Development tools -y
yum -y install zlib-devel
yum install -y openssl-devel libxslt-devel libxml2-devel libcurl-devel
yum install python3 -y    
pip3 install -r xxx
}


#安装node并替换为国内源
sqamt_nodo(){
cd /data/devops/tools
wget https://nodejs.org/download/release/v16.14.2/node-v16.14.2-linux-x64.tar.gz
tar xzvf node-v16.14.2-linux-x64.tar.gz
ln -s /data/devops/tools/node-v16.14.2-linux-x64/bin/node /usr/bin/node
ln -s /data/devops/tools/node-v16.14.2-linux-x64/bin/npm /usr/bin/npm
npm config set registry https://registry.npm.taobao.org
cd -
}



#我这边设置的是139邮箱 其他邮箱请自行设置
sqamt_mutt(){
yum -y install  mutt msmtp  ca-certificates

cat >/root/.msmtprc <<EOF
defaults
auth on
tls on
tls_starttls off
tls_trust_file /etc/ssl/certs/ca-bundle.crt
account freemail
host smtp.139.com
port 465
user 18820304608@139.com
password As@7847165
from 18820304608@139.com
logfile /var/log/msmtp.log
account default:freemail
EOF
chmod 600 ~/.msmtprc
touch /var/log/msmtp.log

 
cat  >/etc/Muttrc  <<EOF
set from="18820304608@139.com"
set sendmail="/bin/msmtp"
set use_from=yes
set realname="lty"
set editor="vi"
set charset="utf-8"
EOF
}


function install-mysql(){
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
yum localinstall mysql57-community-release-el7-11.noarch.rpm
yum install -y mysql-community-server
systemctl enable mysqld
systemctl daemon-reload
systemctl restart mysqld
TEMP_DB_PASSWORD=`grep "temporary password"  /var/log/mysqld.log | awk -F: '{print $NF}' | tr -d '[:space:]'`
mysql -uroot -p$TEMP_DB_PASSWORD --connect-expired-password  << EOF
alter user "root"@"localhost"  identified by "ZBO564knb#";
flush privileges;
EOF
|| echo "mysql安装失败,请自行找原因"
}





#修改好对应参数后,取消对应注释
initEnv

requirements.txt


